package com.zeng.sys.authmd.restfulmodule.configuration;



import com.zeng.sys.authmd.restfulmodule.interceptor.AuthInterceptor;
import com.zeng.sys.authmd.servicemodule.configuration.AuthMdServiceConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;

@Import(AuthMdServiceConfig.class)
@ComponentScan({"com.zeng.sys.authmd.restfulmodule.controller"})
@Configuration
@EnableConfigurationProperties(AuthProperties.class)
public class AuthMdRestfulConfig {

  @Autowired
  private AuthProperties properties;


  @Bean
  public AuthInterceptor authInterceptor() {
    AuthInterceptor authInterceptor = new AuthInterceptor();
    AuthInterceptor.setExclude(properties.getExcludePath());
    return authInterceptor;
  }


  @Bean
  @ConditionalOnExpression("${auth.interceptor.open:true}")
  public WebMvcConfigurer webMvcConfigurer(AuthInterceptor authInterceptor) {
    return new WebMvcConfigurer() {
      @Override
      public void addInterceptors(InterceptorRegistry registry) {
        ArrayList<String> patterns = new ArrayList<>();
        patterns.add("/js/**");
        patterns.add("/css/**");
        patterns.add("/fonts/**");
        patterns.add("/img/**");
        patterns.add("/static/**");
        patterns.add("/*.html");
        patterns.add("/*.ico");
        registry.addInterceptor(authInterceptor).addPathPatterns("/**").excludePathPatterns(patterns);
      }
    };
  }

}
